define(function () {
	var area = {};


	var current2 = {
		prov: '',
		city: '',
		country: '',
		provVal: '',
		cityVal: '',
		countryVal: ''
	};



	showProv2 = function () {
		$('#addr-wrap').empty();
		/*$('#addr-show02').value = '';*/
		// $('.met2').disabled = true;
		$($('#title-wrap').find('LI')[0]).addClass('titleSel');
		var len = provice.length;
		for (var i = 0; i < len; i++) {
			var provLi = document.createElement('li');
			provLi.innerText = provice[i]['name'];
			provLi.index = i;
			$('#addr-wrap').append(provLi);
		}
	}



	/*选择省份之后显示该省下所有城市*/
	showCity2 = function (index) {
		$('#addr-wrap').empty();;
		current2.prov = index;
		current2.provVal = provice[index].name;
		$($('#title-wrap').find('LI')[0]).removeClass('titleSel');
		$($('#title-wrap').find('LI')[1]).addClass('titleSel');
		var cityLen = provice[index].city.length;
		for (var j = 0; j < cityLen; j++) {
			var cityLi = document.createElement('li');
			cityLi.innerText = provice[index].city[j].name;
			cityLi.index = j;
			$('#addr-wrap').append(cityLi);
		}
	}

	/*选择城市之后显示该城市下所有县区*/
	showCountry2 = function (index) {
		$('#addr-wrap').empty();;
		current2.city = index;
		current2.cityVal = provice[current2.prov].city[index].name;
		$($('#title-wrap').find('LI')[1]).removeClass('titleSel');
		$($('#title-wrap').find('LI')[2]).addClass('titleSel');
		var countryLen = provice[current2.prov].city[index].districtAndCounty.length;
		if (countryLen == 0) {
			$('#addr-show02').text(current2.provVal + '-' + current2.cityVal);
		}
		for (var k = 0; k < countryLen; k++) {
			var cityLi = document.createElement('li');
			cityLi.innerText = provice[current2.prov].city[index].districtAndCounty[k];
			cityLi.index = k;
			$('#addr-wrap').append(cityLi);
		}
	}

	/*选中具体的县区*/
	function selectCountry(index) {
		// $('.met2').disabled = false;
		current2.country = index;
		$($('#addr-wrap').find('LI')[index]).css('background', '#23B7E5');
		current2.countryVal = provice[current2.prov].city[current2.city].districtAndCounty[index];

	}

	//初始化地区选择面板
	area.initAreaPanel = function () {
		$('.locatePanel').remove();
		let h5 = `
			<div class='locatePanel'>
				<div id="addr-choice">
					<ul id="title-wrap">
						<li value="0">省份</li>
						<li value="1">城市</li>
						<li value="2">县区</li>
					</ul>
					<div id="show-panel">
						<ul id="addr-wrap">
						</ul>
					</div>
				</div>
				<button type="button" class="btn met2">确定</button>
			</div>
			`;
		$('.body').append(h5);

		area.eventBind();
		showProv2();
	}

	area.eventBind = function () {

		/*************************需要给动态生成的li绑定点击事件********************** */
		$('#addr-wrap').on('click', function (e) {
			var n;
			var e = e || window.event;
			var target = e.target || e.srcElement;
			if (target && target.nodeName == 'LI') {
				/*先判断当前显示区域显示的是省市区的那部分*/
				for (var z = 0; z < 3; z++) {
					if ($($('#title-wrap').find('LI')[z]).hasClass('titleSel'))
						n = z;
				}
				/*显示的处理函数*/
				switch (n) {
					case 0:
						showCity2(target.index);
						break;
					case 1:
						showCountry2(target.index);
						break;
					case 2:
						selectCountry(target.index);
						break;
					default:
						showProv2();
				}
			}
		});

		/*点击确定后恢复成初始状态，且将所选地点显示在输入框中*/
		$('.met2').on('click', function () {
			if(current2.provVal==''){

			}else{
				$('#addr-show02').text('');
				if(current2.provVal===current2.cityVal){
					$('#addr-show02').text(current2.provVal + ' ' + current2.countryVal);
				}else{
					$('#addr-show02').text(current2.provVal + ' ' + current2.cityVal + ' ' + current2.countryVal);
				}
				
				$($('#addr-wrap').find('li')[current2.country]).css('background', '#fff');
			}
			
			$('.locatePanel').remove();
			current2 = {
				prov: '',
				city: '',
				country: '',
				provVal: '',
				cityVal: '',
				countryVal: ''
			};
		});

		/*分别点击省市区标题的处理函数*/
		$('#title-wrap').click(function (e) {
			var e = e || window.event;
			var target = e.target || e.srcElement;
			if (target && target.nodeName == 'LI') {
				for (var z = 0; z < 3; z++) {
					$($('#title-wrap').find('LI')[z]).removeClass('titleSel');
				}
				$(target).addClass('titleSel');
				if ($(target).val() == '0') {
					showProv2();
				} else if ($(target).val() == '1') {
					showCity2(current2.prov);
				} else {
					showCountry2(current2.city);
				}
			}
		})
	}
	return area;
});